package com.huixue.cn.ztej.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.Section;
import com.huixue.cn.ztej.dao.SysSectionMapper;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Service("sysSectionService")
public class SysSectionService {

	private static Logger logger = LoggerFactory.getLogger(SysSectionService.class);

	@Autowired
	private SysSectionMapper sysSectionMapper;


	
	/**
	 * 按条件分页查询记录
	 * @param searchParams	条件
	 * @param pageNumber	页码
	 * @param pageSize		每页数据大小
	 * @return Results
	 */
	public Results searchByPage(Map<String,Object> searchParams,Integer pageNumber, Integer pageSize) {

		Map<String, Object> rmap = new HashMap<String, Object>();
		PageParameter page = new PageParameter();
		page.setCurrentPage(pageNumber);
		page.setPageSize(pageSize);
		searchParams.put("page", page);
		searchParams.put("status", new Integer(1));

		List<Section> list = sysSectionMapper.searchByPage(searchParams);
		
		rmap.put("total", page.getTotalPage());
		rmap.put("page", pageNumber);						//当前页
		rmap.put("records", page.getTotalCount());			//总记录数
		rmap.put("pageSize", pageSize);						//每页个数
		String userName = searchParams.get("userName")==null?"":String.valueOf(searchParams.get("userName"));
		rmap.put("userName", userName);						//当前页
		rmap.put("rows", list);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc(),rmap);
		return results; 
		
	}
	
	/**
	 * 增加工段信息
	 * @param sysProInfo
	 * @return
	 */
	public Results add(Section sysSecInfo){
		Results results = null;
		
		
		sysSecInfo.setCreateTime(new Date());
		sysSectionMapper.saveSection(sysSecInfo);
		
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "工段信息保存成功！");
		return results; 
	}
	
	/**
	 * 根据工段id查询工段基本信息
	 * @param sysUserInfo
	 * @return Results
	 */
	public Results findBySecId(Integer id){

		Section sysSecInfo = sysSectionMapper.findBySecId(id);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc(),sysSecInfo);
		return results; 
	}
	
	/**
	 * 修改工段基本信息
	 * @param sysUserInfo
	 * @return Results
	 */
	public Results modify(Section sysSecInfo){

		sysSectionMapper.update(sysSecInfo);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		return results; 
	}
	
	/**
	 * 删除工段基本信息
	 * @param sysUserInfo
	 * @return Results
	 */
	public Results delete(Integer secId){

		sysSectionMapper.delete(secId);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		return results; 
	}
	
	public List<Section> listSectionByPro(Integer proId){

		List<Section> list = sysSectionMapper.listSectionByPro(proId);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc(),list);
		return list; 
	}
	
}
